// pages/profile/profile.js
import {
  UserModel
} from '../../models/user.js';

import {
  ContentModel
} from '../../models/content.js';

import {
  Lock
} from '../../models/lock.js';

import {
  Token
} from '../../utils/token.js';

const token = new Token();

const user = new UserModel();

const content = new ContentModel();

const lock = new Lock();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: 0,
    user: {},
    defaultBg: "../../images/static/bg.jpeg",
    loadingHidden: true,
    pageIndex: 1,
    contents: [],
    contentsTotal: 0,
    loadMore: {
      "show": false,
      "type": "loading"
    },
    followed: false,
    self: false,
    title: "",
    shareScene: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.userId
    })

    if (options.hasOwnProperty("type")) {
      // 分享场景
      this.setData({
        shareScene: true
      })

      this._commonLoad();
    } else {
      this._commonLoad();
    }
  },
  _commonLoad() {
    // 避免获取不到个人信息
    token.verify().then(res => {
      if (res) {
        this._loadData();
        this._getUserContents();
      }
    });
  },
  onHome() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  _loadData() {

    user.self(this.data.userId).then(res => {
      this.setData({
        self: res.data
      })
    });

    user.getUserInfoById(this.data.userId).then(res => {
      
      let user = res.data;
      user.fansNum = !user.fansNum ? 0 : user.fansNum;
      user.followedNum = !user.followedNum ? 0 : user.followedNum;
      user.wonLikedNum = !user.wonLikedNum ? 0 : user.wonLikedNum;

      this.setData({
        user: user,
        title: user.nickName
      })
    })

    user.followed(this.data.userId).then(res => {
      this.setData({
        followed: res.data
      })

      // 关闭骨架
      this._closeSkeletonView();
    })
  },

  _getUserContents() {
    let that = this;

    content.getUserContents(this.data.userId, this.data.pageIndex).then(res => {

      that.setData({
        contentsTotal: res.data.totalElements
      })

      // 加载数据
      if (res.data.content.length > 0) {
        //数组合并
        that.data.contents.push.apply(that.data.contents, res.data.content);
        that.setData({
          contents: that.data.contents
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.contents.length == this.data.contentsTotal) {
      // 加载全部
      this._showLoadedAllTip();
    } else {
      this.data.pageIndex++;
      this._showLoadMoreTip();
      this._getUserContents();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.title,
      path: `/pages/profile/profile?userId=${this.data.userId}&type=share`,
    }
  },
  _closeSkeletonView() {
    this.setData({
      loadingHidden: false
    });
  },
  _showLoadedAllTip() {
    this.setData({
      loadMore: {
        "show": true,
        "type": "end"
      }
    })
  },
  _showLoadMoreTip() {
    this.setData({
      loadMore: {
        "show": true,
        "type": "loading"
      }
    })
  },
  _closeLoadMoreTip() {
    this.setData({
      loadMore: {
        "show": false,
        "type": "loading"
      }
    })
  },
  toDetail(e) {
    let params = e.detail;
    // 跳转到详情页
    wx.navigateTo({
      url: `../detail/detail?id=${params.id}`,
    });
  },
  onFollow(e) {

    if (lock.isLocked()) {
      return;
    }

    // 加锁
    lock.lock();

    let userId = e.currentTarget.dataset.userId;
    user.follow(userId, this.data.followed).then(res => {

      // followed -> true:已关注,false:关注
      let result = this.data.followed ? !res.data : res.data;
      setTimeout(() => {
        this.setData({
          followed: result
        });

        this._loadData();

      }, 600);

      lock.unlock();
    }).catch(err => {
      console.log(err);
      lock.unlock();
    });
  },
  onUserRelation(e) {
    let userId = e.currentTarget.dataset.userId;
    if (this.data.self) {
      wx.navigateTo({
        url: '../my-list/my-list',
      })
    } else {
      wx.navigateTo({
        url: `../user-list/user-list?userId=${userId}`,
      })
    }
  }
})